<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="tag" uri="surveyonline-taglib" %>

<%@ page import="fr.univ.surveyonline.model.Survey" %>
<%@ page import="fr.univ.surveyonline.model.Contraintes" %>
<%@ page import="fr.univ.surveyonline.model.PropertiesNumber" %>
<%@ page import="fr.univ.surveyonline.model.allEnum.*" %>
<%@ page import="fr.univ.surveyonline.model.Survey" %>
<%@ page import="fr.univ.surveyonline.model.ResponseManager" %>
<%@ page import="fr.univ.surveyonline.datastore.PMF" %>
<%@ page import="java.util.*" %>
<%@ page import="fr.univ.surveyonline.language.Language" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<%@page import="fr.univ.surveyonline.model.DateHelpers"%><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fr" lang="fr">
<head>
    <title><tag:languageTag id="title"/></title>
	<%@ include file="/layout/head.tag" %>
</head>
<body>

<div id="global">
	<div id="entete">
		<%@ include file="/layout/menu.tag" %>
	</div>
	<div id="contenu">
		<h2><tag:languageTag id="contrainte_title"/></h2>
		<% 
		String langue = "FR";
		for (Cookie c : request.getCookies()) {
			if (c.getName().equals("langue")) {
				langue = c.getValue(); 
				break;
			}
		}

		User u = null;
		if (request.getSession().getAttribute("user") == null) {
			String message = (String)Language.getWord("contrainte_be_connected",langue);
			request.setAttribute("message", message);
			RequestDispatcher rd = request.getRequestDispatcher("unaccessible.jsp");
			try {
				rd.forward(request, response);
			} catch (ServletException e) {
				e.printStackTrace();
			}			
		} else {
			u = (User) request.getSession().getAttribute("user");
		}
		
		//List<Survey> s = (List<Survey>) PMF.getAllFromClass(Survey.class);
		if (request.getParameter("survey") == null) {
			RequestDispatcher rd = request.getRequestDispatcher("contraintes-choix.jsp");
			try {
				rd.forward(request, response);
			} catch (ServletException e) {
				e.printStackTrace();
			}	
		} else {
			Survey survey = null;
			long surveyId = Long.valueOf(request.getParameter("survey"));
			survey = (Survey) PMF.getElementById(Survey.class, surveyId);
			Contraintes c = survey.getContraintes();
			//List<User> list = PropertiesNumber.sortUser(User.getAllMembers(), DateHelpers.getTodayDate(),c);	
			// TODO Modifier ici
			List<ResponseManager> resp = (List<ResponseManager>) PMF.getAllFromClass(ResponseManager.class);
			List<User> list = new ArrayList<User>();			
			for (ResponseManager r : resp) {
				if ((r.getSurveyKey().getId() == survey.getKey().getId()) 
						&& r.untilTheEnd()) {
					User user = (User) PMF.getElementById(User.class, r.getUserKey().getId());
					list.add(user);
				}
			}
			
			PropertiesNumber pn = new PropertiesNumber(list); 
			int total = list.size();
			
			String message = (String)Language.getWord("contrainte_not_respected",langue);
			out.println("<p>"+ message +" "+ survey.getName() + "</p>");
			
			if ((!pn.verify(c, SexeEnum.Homme)) || (!pn.verify(c, SexeEnum.Femme))) {
				%>
				<img src="../images/top-cont1.png" alt="" style="margin-left: 150px;" />
				<div style="background: url(../images/bg-cont1.png);padding:1px; padding-left:40px; margin-left: 150px; margin-right: 150px; background-repeat: repeat-y repeat-x;">
				<div align="center"><h3><tag:languageTag id="contrainte_sex"/></h3></div>
				<%
				for (SexeEnum sexe : SexeEnum.values()) {
					if (!pn.verify(c, sexe)) {
						%>
							<h3><%out.print(sexe.toString());%> <tag:languageTag id="contrainte_requis"/></h3>
							<ul style="padding-left:20px; color:white;">
								<li><tag:languageTag id="contrainte_min"/><%if (c.getMinimum(sexe) == null) {out.print("0");} else {out.print(c.getMinimum(sexe));} %></li>
								<li><tag:languageTag id="contrainte_pourcent"/><%if (c.getMinimumPourCent(sexe) == null) {out.print("0");} else {out.print(c.getMinimumPourCent(sexe));} %>%</li>
							</ul>
							<h3><%out.print(sexe.toString());%> <tag:languageTag id="contrainte_obtenu"/></h3>
							<ul style="padding-left:20px; color:white;">
								<li><tag:languageTag id="contrainte_nb"/><%= pn.getNumberPeople(sexe) %></li>
								<li><tag:languageTag id="contrainte_pourcent"/><% if (total == 0) {
									out.print("0");
								} else {
									out.print(pn.getNumberPeople(sexe)*100/total);
								}%>%</li>
							</ul>
							<br />
						<%
					}
				}		
				%></div><img src="../images/bot-cont1.png" alt=""  style="margin-left: 150px;" /><br /><%
			}
			boolean isValid = false;
			for (AgeEnum age : AgeEnum.values()) {
				if (!pn.verify(c, age)) {
					isValid = true;
				}
			}
			if (isValid) {
				%>
				<img src="../images/top-cont2.png" alt="" style="margin-left: 150px;" />
				<div style="background: url(../images/bg-cont2.png);padding:1px; padding-left:20px; margin-left: 150px; margin-right: 150px; background-repeat: repeat-y;">
				<div align="center"><h3><tag:languageTag id="contrainte_age"/></h3></div>
				<%
				for (AgeEnum age : AgeEnum.values()) {
					if (!pn.verify(c, age)) {
						%>
							<h3><%out.print(age.toString());%> <tag:languageTag id="contrainte_requis"/></h3>
							<ul style="padding-left:20px;">
								<li><tag:languageTag id="contrainte_min"/><%if (c.getMinimum(age) == null) {out.print("0");} else {out.print(c.getMinimum(age));}%></li>
								<li><tag:languageTag id="contrainte_pourcent"/><% if (c.getMinimumPourCent(age) == null) {out.print("0");} else {out.print(c.getMinimumPourCent(age));}%>%</li>
							</ul>
							<h3><%out.print(age.toString());%> <tag:languageTag id="contrainte_obtenu"/></h3>
							<ul style="padding-left:20px;">
								<li><tag:languageTag id="contrainte_nb"/><%= pn.getNumberPeople(age) %></li>
								<li><tag:languageTag id="contrainte_pourcent"/><%if (total == 0) {
									out.print("0");
								} else {
									out.print(pn.getNumberPeople(age)*100/total);
								} %>%</li>
							</ul>
							<br />
						<%
					}
				}
				%></div><img src="../images/bot-cont2.png" alt="" style="margin-left: 150px;" /><br /><%
			}
			
			isValid = false;
			for (Region r: Region.values()) {
				if (!pn.verify(c, r)) {
					isValid = true;
				}
			}
			if (isValid) {
				%>
				<img src="../images/top-cont3.png" alt="" style="margin-left: 150px;" />
				<div style="background: url(../images/bg-cont3.png);padding:1px; padding-left:20px; margin-left: 150px; margin-right: 150px; background-repeat: repeat-y;">
				<div align="center"><h3><tag:languageTag id="contrainte_region"/></h3></div>
				<%
				for (Region r : Region.values()) {
					if (!pn.verify(c, r)) {
						%>
							<h3><%out.print(r.toString());%> <tag:languageTag id="contrainte_requis"/></h3>
							<ul style="padding-left:20px;">
								<li><tag:languageTag id="contrainte_min"/><%if (c.getMinimum(r) == null) {out.print("0");} else {out.print(c.getMinimum(r));}%></li>
								<li><tag:languageTag id="contrainte_pourcent"/><%if (c.getMinimumPourCent(r) == null) {out.print("0");} else {out.print(c.getMinimumPourCent(r));}%>%</li>
							</ul>
							<h3><%out.print(r.toString());%> <tag:languageTag id="contrainte_obtenu"/></h3>
							<ul style="padding-left:20px;">
								<li><tag:languageTag id="contrainte_nb"/><%= pn.getNumberPeople(r) %></li>
								<li><tag:languageTag id="contrainte_pourcent"/><%if (total == 0) {
									out.print("0");
								} else {
									out.print(pn.getNumberPeople(r)*100/total);
								} %>%</li>
							</ul>
							<br />
						<%
					}
				}
				%></div><img src="../images/bot-cont3.png" alt="" style="margin-left: 150px;" /><br /><%
			}	
			
			isValid = false;
			for (Categorie cat : Categorie.values()) {
				if (!pn.verify(c, cat)) {
					isValid = true;
				}
			}
			if (isValid) {
				%>
				<img src="../images/top-cont1.png" alt="" style="margin-left: 150px;" />
				<div style="background: url(../images/bg-cont1.png);padding:1px; padding-left:20px; margin-left: 150px; margin-right: 150px; background-repeat: repeat-y;">
				<div align="center"><h3><tag:languageTag id="contrainte_categ"/></h3></div>
				<%
				for (Categorie cat : Categorie.values()) {
					if (!pn.verify(c, cat)) {
						%>
							<h3><%out.print(cat.toString());%> <tag:languageTag id="contrainte_requis"/></h3>
							<ul style="padding-left:20px; color:white;">
								<li><tag:languageTag id="contrainte_min"/><%if (c.getMinimum(cat) == null) {out.print("0");} else {out.print(c.getMinimum(cat));} %></li>
								<li><tag:languageTag id="contrainte_pourcent"/><%if (c.getMinimumPourCent(cat) == null) {out.print("0");} else {out.print(c.getMinimumPourCent(cat));} %>%</li>
							</ul>
							<h3><%out.print(cat.toString());%> <tag:languageTag id="contrainte_obtenu"/></h3>
							<ul style="padding-left:20px; color:white;">
								<li><tag:languageTag id="contrainte_nb"/><%= pn.getNumberPeople(cat) %></li>
								<li><tag:languageTag id="contrainte_pourcent"/><%if (total == 0) {
									out.print("0");
								} else {
									out.print(pn.getNumberPeople(cat)*100/total);
								}%>%</li>
							</ul>
							<br />
						<%
					}
				}
				%></div><img src="../images/bot-cont1.png" alt="" style="margin-left: 150px;" /><br /><%
			}
	
			%>
				<img src="../images/top-cont3.png" alt="" style="margin-left: 150px;" />
				<div style="background: url(../images/bg-cont3.png); padding:20px; padding-top:10px; padding-left:20px; margin-left: 150px; margin-right: 150px; background-repeat: repeat-y;">
					<h3 align="center"><tag:languageTag id="contrainte_choose"/></h3>
					<form action="/chooseConstraint" method="get">
					<ul>
						<li><input type="radio" name="choix" value="publish" checked="checked"/><tag:languageTag id="contrainte_put"/></li>
						<li><input type="radio" name="choix" value="delete"/><tag:languageTag id="contrainte_leave"/></li>
						<li><input type="radio" name="choix" value="load"/><tag:languageTag id="contrainte_reload"/></li>
						<li><input type="submit" value="<tag:languageTag id="contrainte_submit"/>"/></li>
					</ul>
					<input type="hidden" name="survey" value="<%=survey.getKey().getId()%>"/>
					</form>
				</div>
				<img src="../images/bot-cont3.png" alt="" style="margin-left: 150px;" />
			<%
		}

		%>
		
		<div class="clear"></div>
	</div>
	<div id="pied">
		<%@ include file="/layout/pied.tag" %>
	</div>
</div>

</body>
</html>